package com.shanghai.shanghaielectric.Model;

import android.os.Parcel;
import android.os.Parcelable;
import android.widget.Toast;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.MyApplication;

import java.util.Map;

import shanghaielectric_lib.EnumDataHelper;
import shanghaielectric_lib.RDBHelper.Cell;
import shanghaielectric_lib.RDBHelper.RDBHelper;
import shanghaielectric_lib.Utils.LogUtil;
import shanghaielectric_lib.Utils.dwin.BitUtils;

/**
 * Created by 45000460 on 2017/11/27.
 */

public class WriteDataModel implements Parcelable {
    public EnumDataHelper.EnumWriteData_Command controlCommand;
    public int deviceModel;
    public int unitEnable;
    public int totalCapacity;
    public int ctSystemOrLoad;
    public int ctSystemOrLoadProperty;//网侧，正方向
    public int ctCabinet;//
    public int phaseOffset1;
    public int phaseOffset3;
    public int phaseOffset5;
    public int phaseOffset7;
    public int phaseOffset9;
    public int phaseOffset11;
    public int phaseOffset13;
    public int phaseOffset15;
    public int phaseOffset17;
    public int amPercent1;
    public int amPercent3;
    public int amPercent5;
    public int amPercent7;
    public int amPercent9;
    public int amPercent11;
    public int amPercent13;
    public int amPercent15;
    public int amPercent17;
    public int highHarmonicSelect1;
    public int highHarmonicSelect2;
    public int powerFactorSet;
    public int qPercentSet;
    public int thdLimitU;
    public int thdLimitI;
    public int urmsMax;
    public int urmsMin;
    public int irmsMax;
    public int deviceType;
    public int capacitorSwitchDelay;
    public int hysteresysBanWidth;
    public int passiveCapacitorGroup;//对应的是240  300  360 这三个状态
    public int switchInterval;
    public int cleareSwitchTimes;
    public int testCapacitor1;
    public int testCapacitor2;

    public WriteDataModel() {
        try {

        Map<Integer, Cell> cellMap_write = RDBHelper.getInstance().getCellsCollection(1);

//        controlCommand = EnumDataHelper.EnumWriteData_Command.getEnumWriteData_Command( String.valueOf(cellMap_write.get(89).realValue));
            controlCommand=EnumDataHelper.EnumWriteData_Command.CLEAR;
        unitEnable = Integer.valueOf(String.valueOf(cellMap_write.get(90).realValue));
        LogUtil.e("init",""+unitEnable);
        totalCapacity = Integer.valueOf(String.valueOf( cellMap_write.get(91).realValue));
        cleareSwitchTimes = Integer.valueOf(String.valueOf( cellMap_write.get(92).realValue));
        testCapacitor1 = Integer.valueOf(String.valueOf( cellMap_write.get(93).realValue));
        testCapacitor2 = Integer.valueOf(String.valueOf( cellMap_write.get(94).realValue));



        deviceModel = Integer.valueOf(String.valueOf( cellMap_write.get(99).realValue));
        ctSystemOrLoad = Integer.valueOf(String.valueOf( cellMap_write.get(100).realValue));
        ctSystemOrLoadProperty = Integer.valueOf(String.valueOf( cellMap_write.get(101).realValue));
        ctCabinet = Integer.valueOf(String.valueOf( cellMap_write.get(102).realValue));
        highHarmonicSelect1 = Integer.valueOf(String.valueOf( cellMap_write.get(103).realValue));
        highHarmonicSelect2 = Integer.valueOf(String.valueOf( cellMap_write.get(104).realValue));
        powerFactorSet = Integer.valueOf(String.valueOf( cellMap_write.get(105).realValue));
        qPercentSet = Integer.valueOf(String.valueOf( cellMap_write.get(106).realValue));
        thdLimitU = Integer.valueOf(String.valueOf( cellMap_write.get(107).realValue));
        thdLimitI = Integer.valueOf(String.valueOf( cellMap_write.get(108).realValue));
        phaseOffset1 = Integer.valueOf(String.valueOf( cellMap_write.get(109).realValue));
        phaseOffset3 = Integer.valueOf(String.valueOf( cellMap_write.get(110).realValue));
        phaseOffset5 = Integer.valueOf(String.valueOf( cellMap_write.get(111).realValue));
        phaseOffset7 = Integer.valueOf(String.valueOf( cellMap_write.get(112).realValue));
        phaseOffset9 = Integer.valueOf(String.valueOf( cellMap_write.get(113).realValue));
        phaseOffset11 = Integer.valueOf(String.valueOf( cellMap_write.get(114).realValue));
        phaseOffset13 = Integer.valueOf(String.valueOf( cellMap_write.get(115).realValue));
        phaseOffset15 = Integer.valueOf(String.valueOf( cellMap_write.get(116).realValue));
        phaseOffset17 = Integer.valueOf(String.valueOf( cellMap_write.get(117).realValue));

        amPercent1 = Integer.valueOf(String.valueOf( cellMap_write.get(119).realValue));
        amPercent3 = Integer.valueOf(String.valueOf( cellMap_write.get(120).realValue));
        amPercent5 = Integer.valueOf(String.valueOf( cellMap_write.get(121).realValue));
        amPercent7 =Integer.valueOf(String.valueOf( cellMap_write.get(122).realValue));
        amPercent9 = Integer.valueOf(String.valueOf( cellMap_write.get(123).realValue));
        amPercent11 = Integer.valueOf(String.valueOf( cellMap_write.get(124).realValue));
        amPercent13 =Integer.valueOf(String.valueOf( cellMap_write.get(125).realValue));
        amPercent15 = Integer.valueOf(String.valueOf( cellMap_write.get(126).realValue));
        amPercent17 = Integer.valueOf(String.valueOf( cellMap_write.get(127).realValue));

        urmsMax =Integer.valueOf(String.valueOf( cellMap_write.get(129).realValue));
        urmsMin = Integer.valueOf(String.valueOf( cellMap_write.get(130).realValue));
        irmsMax = Integer.valueOf(String.valueOf(cellMap_write.get(131).realValue));
        deviceType = Integer.valueOf(String.valueOf( cellMap_write.get(132).realValue));
        capacitorSwitchDelay = Integer.valueOf(String.valueOf( cellMap_write.get(133).realValue));
        hysteresysBanWidth = Integer.valueOf(String.valueOf( cellMap_write.get(134).realValue));
        passiveCapacitorGroup = Integer.valueOf(String.valueOf( cellMap_write.get(135).realValue));
        switchInterval = Integer.valueOf(String.valueOf( cellMap_write.get(136).realValue));
        }catch (Exception e){
            Toast.makeText(MyApplication.getContext(), "WriteDataModel 初始化失败"+e.toString(), Toast.LENGTH_SHORT).show();
            LogUtil.e("WriteDataModel","WriteDataModel初始化失败"+e.toString());
        }
    }
    public EnumDataHelper.EnumWriteData_Command getControlCommand() {
        return controlCommand;
    }

    public void setControlCommand(EnumDataHelper.EnumWriteData_Command controlCommand) {
        this.controlCommand = controlCommand;
    }

    public int getDeviceModel() {
        return deviceModel;
    }

    public void setDeviceModel(int deviceModel) {
        this.deviceModel = deviceModel;
    }

    public int getUnitEnable() {
        return unitEnable;
    }

    public void setUnitEnable(int unitEnable) {
        this.unitEnable = unitEnable;
    }

    public void setUnitEnable_index(int deviceID, boolean flag) {
        int result = Constants.writeDataModel.unitEnable;
        this.unitEnable = flag ? BitUtils.setBit_1(result, deviceID - 1) : BitUtils.setBit_0(result, deviceID - 1);
        LogUtil.e("enable-writeDataModel",Integer.toBinaryString(result)+"---deviceID"+(deviceID-1)+"---"+Integer.toBinaryString(unitEnable));
    }

    public int getTotalCapacity() {
        return totalCapacity;
    }

    public void setTotalCapacity(int totalCapacity) {
        this.totalCapacity = totalCapacity;
    }

    public int getCtSystemOrLoad() {
        return ctSystemOrLoad;
    }

    public void setCtSystemOrLoad(int ctSystemOrLoad) {
        this.ctSystemOrLoad = ctSystemOrLoad;
    }

    public int getCtSystemOrLoadProperty_position() {//取到bit0
        return ctSystemOrLoadProperty&1;
    }
    public int getCtSystemOrLoadProperty_direction() {//取到bit2
        return ctSystemOrLoadProperty>>2;
    }

    public void setCtSystemOrLoadProperty(int gridOrLoad, int deriction) {
        int result = Constants.writeDataModel.ctSystemOrLoadProperty;
        if (gridOrLoad == 0) {
            if (deriction == 0) {
                result = 2;
            } else {
                result = 4;
            }
        } else {
            if (deriction == 0) {
                result = 1;
            } else {
                result = 5;
            }
        }
        this.ctSystemOrLoadProperty = result;
    }

    public int getCtCabinet() {
        return ctCabinet;
    }

    public void setCtCabinet(int ctCabinet) {
        this.ctCabinet = ctCabinet;
    }

    public int getPhaseOffset1() {
        return phaseOffset1;
    }

    public void setPhaseOffset1(int phaseOffset1) {
        this.phaseOffset1 = phaseOffset1;
    }

    public int getPhaseOffset3() {
        return phaseOffset3;
    }

    public void setPhaseOffset3(int phaseOffset3) {
        this.phaseOffset3 = phaseOffset3;
    }

    public int getPhaseOffset5() {
        return phaseOffset5;
    }

    public void setPhaseOffset5(int phaseOffset5) {
        this.phaseOffset5 = phaseOffset5;
    }

    public int getPhaseOffset7() {
        return phaseOffset7;
    }

    public void setPhaseOffset7(int phaseOffset7) {
        this.phaseOffset7 = phaseOffset7;
    }

    public int getPhaseOffset9() {
        return phaseOffset9;
    }

    public void setPhaseOffset9(int phaseOffset9) {
        this.phaseOffset9 = phaseOffset9;
    }

    public int getPhaseOffset11() {
        return phaseOffset11;
    }

    public void setPhaseOffset11(int phaseOffset11) {
        this.phaseOffset11 = phaseOffset11;
    }

    public int getPhaseOffset13() {
        return phaseOffset13;
    }

    public void setPhaseOffset13(int phaseOffset13) {
        this.phaseOffset13 = phaseOffset13;
    }

    public int getPhaseOffset15() {
        return phaseOffset15;
    }

    public void setPhaseOffset15(int phaseOffset15) {
        this.phaseOffset15 = phaseOffset15;
    }

    public int getPhaseOffset17() {
        return phaseOffset17;
    }

    public void setPhaseOffset17(int phaseOffset17) {
        this.phaseOffset17 = phaseOffset17;
    }

    public int getAmPercent1() {
        return amPercent1;
    }

    public void setAmPercent1(int amPercent1) {
        this.amPercent1 = amPercent1;
    }

    public int getAmPercent3() {
        return amPercent3;
    }

    public void setAmPercent3(int amPercent3) {
        this.amPercent3 = amPercent3;
    }

    public int getAmPercent5() {
        return amPercent5;
    }

    public void setAmPercent5(int amPercent5) {
        this.amPercent5 = amPercent5;
    }

    public int getAmPercent7() {
        return amPercent7;
    }

    public void setAmPercent7(int amPercent7) {
        this.amPercent7 = amPercent7;
    }

    public int getAmPercent9() {
        return amPercent9;
    }

    public void setAmPercent9(int amPercent9) {
        this.amPercent9 = amPercent9;
    }

    public int getAmPercent11() {
        return amPercent11;
    }

    public void setAmPercent11(int amPercent11) {
        this.amPercent11 = amPercent11;
    }

    public int getAmPercent13() {
        return amPercent13;
    }

    public void setAmPercent13(int amPercent13) {
        this.amPercent13 = amPercent13;
    }

    public int getAmPercent15() {
        return amPercent15;
    }

    public void setAmPercent15(int amPercent15) {
        this.amPercent15 = amPercent15;
    }

    public int getAmPercent17() {
        return amPercent17;
    }

    public void setAmPercent17(int amPercent17) {
        this.amPercent17 = amPercent17;
    }

    public boolean getHighHarmonicSelect1(int i) {
        return BitUtils.getBit(highHarmonicSelect1,i);
    }

    //从0开始
    public void setHighHarmonicSelect1(int i, boolean flag) {
        int result = highHarmonicSelect1;
        result = flag ? BitUtils.setBit_1(result, i) : BitUtils.setBit_0(result, i);
        this.highHarmonicSelect1 = result;
    }

    public boolean getHighHarmonicSelect2(int i) {
        return BitUtils.getBit(highHarmonicSelect2,i);
    }

    public void setHighHarmonicSelect2(int i, boolean flag) {
        int result = highHarmonicSelect2;
        result = flag ? BitUtils.setBit_1(result, i) : BitUtils.setBit_0(result, i);
        this.highHarmonicSelect2 = result;
    }

    public int getPowerFactorSet() {
        return powerFactorSet;
    }

    public void setPowerFactorSet(int powerFactorSet) {
        this.powerFactorSet = powerFactorSet;
    }

    public int getqPercentSet() {
        return qPercentSet;
    }

    public void setqPercentSet(int qPercentSet) {
        this.qPercentSet = qPercentSet;
    }

    public int getThdLimitU() {
        return thdLimitU;
    }

    public void setThdLimitU(int thdLimitU) {
        this.thdLimitU = thdLimitU;
    }

    public int getThdLimitI() {
        return thdLimitI;
    }

    public void setThdLimitI(int thdLimitI) {
        this.thdLimitI = thdLimitI;
    }

    public int getUrmsMax() {
        return urmsMax;
    }

    public void setUrmsMax(int urmsMax) {
        this.urmsMax = urmsMax;
    }

    public int getUrmsMin() {
        return urmsMin;
    }

    public void setUrmsMin(int urmsMin) {
        this.urmsMin = urmsMin;
    }

    public int getIrmsMax() {
        return irmsMax;
    }

    public void setIrmsMax(int irmsMax) {
        this.irmsMax = irmsMax;
    }

    public int getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(int deviceType) {
        this.deviceType = deviceType;
    }

    public int getCapacitorSwitchDelay() {
        return capacitorSwitchDelay;
    }

    public void setCapacitorSwitchDelay(int capacitorSwitchDelay) {
        this.capacitorSwitchDelay = capacitorSwitchDelay;
    }

    public int getHysteresysBanWidth() {
        return hysteresysBanWidth;
    }

    public void setHysteresysBanWidth(int hysteresysBanWidth) {
        this.hysteresysBanWidth = hysteresysBanWidth;
    }

    public int getPassiveCapacitorGroup() {
        return passiveCapacitorGroup-1;
    }

    public void setPassiveCapacitorGroup(int passiveCapacitorGroup) {
        this.passiveCapacitorGroup = passiveCapacitorGroup;
    }

    public int getSwitchInterval() {
        return switchInterval;
    }

    public void setSwitchInterval(int switchInterval) {
        this.switchInterval = switchInterval;
    }

    public int getCleareSwitchTimes() {
        return cleareSwitchTimes;
    }

    public void setCleareSwitchTimes(int cleareSwitchTimes) {
        this.cleareSwitchTimes = cleareSwitchTimes;
    }

    public boolean getTestCapacitor1(int i) {
        return BitUtils.getBit(testCapacitor1,i);
    }

    public void setTestCapacitor1(int i,boolean flag) {
        int result = testCapacitor1;
        result = flag ? BitUtils.setBit_1(result, i) : BitUtils.setBit_0(result, i);
        this.testCapacitor1 = result;

    }
    public void setTestCapacitor1(int testCapacitor1) {
        this.testCapacitor1 = testCapacitor1;
    }
    public int getTestCapacitor2() {
        return testCapacitor2;
    }

    public void setTestCapacitor2(int testCapacitor2) {
        this.testCapacitor2 = testCapacitor2;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.controlCommand == null ? -1 : this.controlCommand.ordinal());
        dest.writeInt(this.deviceModel);
        dest.writeInt(this.unitEnable);
        dest.writeInt(this.totalCapacity);
        dest.writeInt(this.ctSystemOrLoad);
        dest.writeInt(this.ctSystemOrLoadProperty);
        dest.writeInt(this.ctCabinet);
        dest.writeInt(this.phaseOffset1);
        dest.writeInt(this.phaseOffset3);
        dest.writeInt(this.phaseOffset5);
        dest.writeInt(this.phaseOffset7);
        dest.writeInt(this.phaseOffset9);
        dest.writeInt(this.phaseOffset11);
        dest.writeInt(this.phaseOffset13);
        dest.writeInt(this.phaseOffset15);
        dest.writeInt(this.phaseOffset17);
        dest.writeInt(this.amPercent1);
        dest.writeInt(this.amPercent3);
        dest.writeInt(this.amPercent5);
        dest.writeInt(this.amPercent7);
        dest.writeInt(this.amPercent9);
        dest.writeInt(this.amPercent11);
        dest.writeInt(this.amPercent13);
        dest.writeInt(this.amPercent15);
        dest.writeInt(this.amPercent17);
        dest.writeInt(this.highHarmonicSelect1);
        dest.writeInt(this.highHarmonicSelect2);
        dest.writeInt(this.powerFactorSet);
        dest.writeInt(this.qPercentSet);
        dest.writeInt(this.thdLimitU);
        dest.writeInt(this.thdLimitI);
        dest.writeInt(this.urmsMax);
        dest.writeInt(this.urmsMin);
        dest.writeInt(this.irmsMax);
        dest.writeInt(this.deviceType);
        dest.writeInt(this.capacitorSwitchDelay);
        dest.writeInt(this.hysteresysBanWidth);
        dest.writeInt(this.passiveCapacitorGroup);
        dest.writeInt(this.switchInterval);
        dest.writeInt(this.cleareSwitchTimes);
        dest.writeInt(this.testCapacitor1);
        dest.writeInt(this.testCapacitor2);
    }

    protected WriteDataModel(Parcel in) {
        int tmpControlCommand = in.readInt();
        this.controlCommand = tmpControlCommand == -1 ? null : EnumDataHelper.EnumWriteData_Command.values()[tmpControlCommand];
        this.deviceModel = in.readInt();
        this.unitEnable = in.readInt();
        this.totalCapacity = in.readInt();
        this.ctSystemOrLoad = in.readInt();
        this.ctSystemOrLoadProperty = in.readInt();
        this.ctCabinet = in.readInt();
        this.phaseOffset1 = in.readInt();
        this.phaseOffset3 = in.readInt();
        this.phaseOffset5 = in.readInt();
        this.phaseOffset7 = in.readInt();
        this.phaseOffset9 = in.readInt();
        this.phaseOffset11 = in.readInt();
        this.phaseOffset13 = in.readInt();
        this.phaseOffset15 = in.readInt();
        this.phaseOffset17 = in.readInt();
        this.amPercent1 = in.readInt();
        this.amPercent3 = in.readInt();
        this.amPercent5 = in.readInt();
        this.amPercent7 = in.readInt();
        this.amPercent9 = in.readInt();
        this.amPercent11 = in.readInt();
        this.amPercent13 = in.readInt();
        this.amPercent15 = in.readInt();
        this.amPercent17 = in.readInt();
        this.highHarmonicSelect1 = in.readInt();
        this.highHarmonicSelect2 = in.readInt();
        this.powerFactorSet = in.readInt();
        this.qPercentSet = in.readInt();
        this.thdLimitU = in.readInt();
        this.thdLimitI = in.readInt();
        this.urmsMax = in.readInt();
        this.urmsMin = in.readInt();
        this.irmsMax = in.readInt();
        this.deviceType = in.readInt();
        this.capacitorSwitchDelay = in.readInt();
        this.hysteresysBanWidth = in.readInt();
        this.passiveCapacitorGroup = in.readInt();
        this.switchInterval = in.readInt();
        this.cleareSwitchTimes = in.readInt();
        this.testCapacitor1 = in.readInt();
        this.testCapacitor2 = in.readInt();
    }

    public static final Parcelable.Creator<WriteDataModel> CREATOR = new Parcelable.Creator<WriteDataModel>() {
        @Override
        public WriteDataModel createFromParcel(Parcel source) {
            return new WriteDataModel(source);
        }

        @Override
        public WriteDataModel[] newArray(int size) {
            return new WriteDataModel[size];
        }
    };
}
